package apig.utils;

import java.io.*;

/**
 * <b>Note:</b> Thread SAFE.
 * <p>
 * <b>Note:</b> Doesn't create threads.
 * 
 * @author Alex
 * 
 */
public class DoubledStream extends OutputStream {
	private final OutputStream os1;
	private final OutputStream os2;

	public DoubledStream(OutputStream os1, OutputStream os2) {
		this.os1 = os1;
		this.os2 = os2;
	}

	@Override
	public void write(int b) throws IOException {
		os1.write(b);
		os2.write(b);
	}

	@Override
	public void write(byte[] b) throws IOException {
		os1.write(b);
		os2.write(b);
	}

	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		os1.write(b, off, len);
		os2.write(b, off, len);
	}

	@Override
	public void flush() throws IOException {
		os1.flush();
		os2.flush();
	}

	@Override
	public void close() throws IOException {
		IOException ex = null;
		try {
			os1.close();
		} catch (IOException e) {
			ex = e;
		}
		try {
			os2.close();
		} catch (IOException e) {
			ex = e;
		}
		if (ex != null)
			throw ex;
	}
}
